package com.htfyun.ft.fragment.factory;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.htfyun.ft.R;
import com.htfyun.ft.config.FTProperty;
import com.htfyun.hardware.KeyEventController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FragmentKeyAndroid extends FactoryBaseFragment {

    private TextView txtKeyCode;

    private GridView gridView;
    private KeyAdapter keyAdapter;

    private List<Integer> keyDownList = new ArrayList<>();
    private List<Integer> keyUpList = new ArrayList<>();

    private List<Integer> keysList = new ArrayList<>();

    private SparseArray< String> nameMap = new SparseArray<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_factory_key;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {
        super.onInitView(savedInstanceState);

        gridView = (GridView) findViewById(R.id.gridView);

        keysList.add(KeyEvent.KEYCODE_VOLUME_UP);
        keysList.add(KeyEvent.KEYCODE_VOLUME_DOWN);

        for (Integer i : keysList) {
            switch (i) {
                case KeyEvent.KEYCODE_VOLUME_UP:
                    nameMap.put(i, getString(R.string.test_keys_volume_up));
                    break;
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                    nameMap.put(i, getString(R.string.test_keys_volume_down));
                    break;
                    default:break;
            }
        }

        keyAdapter = new KeyAdapter(getContext(), -1, keysList);

        gridView.setAdapter(keyAdapter);

        txtKeyCode = (TextView) findViewById(R.id.txtKeyCode);

        setBtnPassVisibility(View.GONE);

    }

    @Override
    protected void onVisible() {
        super.onVisible();
        FTProperty.setHtfyunFTEnabled(true);

        keyDownList.clear();
        keyUpList.clear();

        keyDownList.addAll(keysList);
        keyUpList.addAll(keysList);

        keyAdapter.notifyDataSetChanged();

    }

    @Override
    protected void onInvisible() {
        super.onInvisible();
        FTProperty.setHtfyunFTEnabled(false);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean ret = super.onKeyDown(keyCode, event);

        if (!isVisible()) {
            return ret;
        }

        keyDownList.remove(Integer.valueOf(event.getKeyCode()));
        keyAdapter.notifyDataSetChanged();


        String text = txtKeyCode.getText().toString();
        if (!TextUtils.isEmpty(text)) {
            text += "\n";

            String[] all = text.split("\n");
            if (all.length > 5) {
                text = "";
            }
        }
        String status = getString(R.string.test_key_current_keycode_status_down);
        String name = nameMap.get(event.getKeyCode());
        text += getString(R.string.test_key_current_keycode, keyCode, status, name);
        txtKeyCode.setText(text);

        return true;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        boolean ret = super.onKeyUp(keyCode, event);

        if (!isVisible()) {
            return ret;
        }

        keyUpList.remove(Integer.valueOf(event.getKeyCode()));
        keyAdapter.notifyDataSetChanged();

        String text = txtKeyCode.getText().toString();
        if (!TextUtils.isEmpty(text)) {
            text += "\n";

            String[] all = text.split("\n");
            if (all.length > 5) {
                text = "";
            }
        }
        String status = getString(R.string.test_key_current_keycode_status_up);
        String name = nameMap.get(event.getKeyCode());
        text += getString(R.string.test_key_current_keycode, keyCode, status, name);
        txtKeyCode.setText(text);

        if (keyUpList.isEmpty() && keyDownList.isEmpty()) {

            delayAutoPass();

        }

        return true;
    }

    private final class KeyAdapter extends ArrayAdapter<Integer> {


        public KeyAdapter(@NonNull Context context, int resource, @NonNull List<Integer> objects) {
            super(context, resource, objects);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView != null) {
                holder = (ViewHolder)convertView.getTag();
            } else {
                holder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.item_keys, null);
                holder.txt = (TextView) convertView.findViewById(R.id.txtKeys);

                convertView.setTag(holder);
            }

            Integer key = getItem(position);
            if (key != null) {

                String keyInfo = nameMap.get(key);

                if (!keyUpList.contains(key) && !keyDownList.contains(key)) {

                    keyInfo += getString(R.string.arrow) + getString(R.string.test_result_pass);

                    holder.txt.setBackgroundColor(Color.GREEN);

                } else if (!keyDownList.contains(key)) {

                    holder.txt.setBackgroundColor(Color.YELLOW);

                } else if (!keyUpList.contains(key)) {

                    holder.txt.setBackgroundColor(Color.CYAN);

                } else {

                    holder.txt.setBackgroundColor(Color.GRAY);

                }

                holder.txt.setText(keyInfo);
            }
            return convertView;
        }
    }

    private final class ViewHolder {
        TextView txt;
    }
}